<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;

use app\admin\model\Settlement;
use think\Db;

/**
 * 结算单-服务类
 * @author ZhangXuanNeng
 * @since 2024/01/18
 * Class SettlementService
 * @package app\admin\service
 */
class SettlementService extends BaseService
{
    /**
     * 构造函数
     * @author ZhangXuanNeng
     * @since 2020/11/19
     * UserService constructor.
     */
    public function __construct()
    {
        $this->model = new Settlement();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2020/11/19
     * @author ZhangXuanNeng
     */
    public function getList()
    {
        $param = request()->param();
        // 查询条件
        $map = [];

        if(isset($param['promote_id']) && !empty($param['promote_id'])){
            $map[] = ['promote_id','=',$param['promote_id']];
        }
        if(isset($param['promote_1_id']) && !empty($param['promote_1_id'])){
            $map[] = ['promote_1_id','=',$param['promote_1_id']];
        }
        if(isset($param['status']) && !empty($param['status'])){
            $map[] = ['status','=',$param['status']];
        }
        if(isset($param['record_date'])){
            $map[] = ['starttime','>=',$param['record_date'][0]];
            $map[] = ['endtime','<=',$param['record_date'][1]];
        }
        if(isset($param['create_time']) && !empty($param['create_time'])){
            $map[] = ['create_time','between time',$param['create_time']];
        }
        $res = parent::getListNewNoCache($map, "id desc");
        $res['allData'] = [
            'allMoney'  => 0,
            'pageMoney' => 0
        ];
        $allMoney = $this->model
            ->where($map)
            ->sum('sum_money');
        $res['allData']['allMoney'] = $allMoney;
        $pageMoney = 0;
        $new_data = [];
        foreach ($res['data'] as $k=>&$v){
            $pageMoney += $v['sum_money'];
            $is_default = Db::name('settlement_account')
                ->where(['promote_id'=>$v['promote_id'],'is_default'=>1])
                ->find();
            $v['record_date'] = $v['starttime'].'至'.$v['endtime'];
            if(!$is_default){
                $v['is_default'] = $is_default ? 1 : 0;
                $new_data[] = $v->toArray();
                unset($res['data'][$k]);
            }
        }
        $res['data'] = array_merge($new_data,$res['data']->toArray());
        $res['allData']['pageMoney'] = $pageMoney;
        return $res;
    }

    /**
     * 结算单详情
     * @return mixed
     * @since 2024/01/19
     * @author ZhangXuanNeng
     */
    public function getDetails()
    {
        $params = request()->param();
        $res = $this->model->getDetails($params);
        return $res;
    }
    /**
     * 撤销结算
     * @return mixed
     * @since 2024/01/19
     * @author ZhangXuanNeng
     */
    public function setRevoke()
    {
        $params = request()->param();
        $res = $this->model->setRevoke($params);
        return $res;
    }
    /**
     * 生成对账单
     * @return mixed
     * @since 2024/01/19
     * @author ZhangXuanNeng
     */
    public function setStatement()
    {
        $params = request()->param();
        $res = $this->model->setStatement($params);
        return $res;
    }
}